import requests
import json

#  百度翻译

if __name__ == '__main__':
    # UA伪装
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
    }

    # url
    post_url = "https://fanyi.baidu.com/v2transapi?from=en&to=zh"
    data = {
        "from": "en",
        "to": "zh",
        "query": "dog",
        "transtype": "realtime",
        "simple_means_flag": "3",
        "sign": "871501.634748",
        "token": "bbc699fa652dff579c4d166c58f14f3d",
        "domain": "common",
        "ts": "1703840292107"
    }
    response = requests.post(url=post_url, data=data, headers=headers)
    # 获取响应数据 : json()返回的是obj(如果确认响应数据是json类型的才可以使用json()方法
    dic_obj = response.json()
    print(dic_obj)
    # 持久化存储
    fp = open("./dog.json","w",encoding="utf-8")
    json.dump(dic_obj,fp=fp,ensure_ascii=False)
    print("over")